package bluejack.game.gal.diaplay;
import bluejack.game.gal.events.SelectEvent;
import nme.Assets;
import nme.display.Bitmap;
import nme.display.Sprite;
import nme.events.TextEvent;
import nme.events.TimerEvent;
import nme.text.TextField;
import nme.text.TextFormat;
import nme.text.TextFormatAlign;
import nme.utils.Timer;

/**
 * ...
 * @author BlueJack
 */

class TextWin extends Sprite
{
	public var isComplete:Bool;
	private var bg:Bitmap;
	private var mainText:TextField;
	private var nameBg:Bitmap;
	private var nameText:TextField;
	private var downArrow:Bitmap;
	private var colorReg:EReg;
	private var tf:TextFormat;
	private var text:String;
	private var tmpText:String;
	private var blockText:String;
	private var timer:Timer;
	private var defaultColor:Int;

	public function new() {
		super();
		init();
	}
	
	public function setDelay(delay:Int):Void {
		timer = new Timer(delay);
	}
	
	private function init():Void {
		defaultColor = 0x003366;
		bg = new Bitmap(Assets.getBitmapData("ui/textBg.png"));
		bg.x = 20;
		bg.y = 20;
		addChild(bg);
		mainText = new TextField();
		mainText.x = 40;
		mainText.y = 30;
		addChild(mainText);
		//mainText.border = true;
		//mainText.borderColor = 0xff0000;
		mainText.multiline = true;
		mainText.selectable = false;
		mainText.wordWrap = true;
		mainText.width = 720;
		mainText.height = 100;
		nameBg = new Bitmap(Assets.getBitmapData("ui/nameBg.png"));
		nameBg.x = 40;
		nameBg.visible = false;
		addChild(nameBg);
		nameText = new TextField();
		nameText.x = 42;
		nameText.y = 2;
		addChild(nameText);
		//nameText.border = true;
		//nameText.borderColor = 0xff0000;
		tf = new TextFormat("黑体", 20, 0xffffff);
		tf.align = TextFormatAlign.CENTER;
		nameText.defaultTextFormat = tf;
		nameText.selectable = false;
		nameText.width = 106;
		nameText.height = 24;
		nameText.visible = false;
		downArrow = new Bitmap(Assets.getBitmapData("ui/down_2.png"));
		downArrow.x = 750;
		downArrow.y = 114;
		downArrow.visible = false;
		addChild(downArrow);
		tf.font = "Arial";
		//tf.color = 0x003366;
		tf.size = 18;
		tf.align = TextFormatAlign.LEFT;
		mainText.defaultTextFormat = tf;
		setStyle();
		setDelay(20);
	}
	
	public function showName(str:String,color:Int):Void {
		if (str != null) {
			nameText.textColor = color;
			nameText.text = str;
			nameText.visible = true;
			nameBg.visible = true;
		}else {
			nameText.visible = false;
			nameBg.visible = false;
		}
	}
	
	public function setStyle(position:Int = 2, showSkin:Bool = true):Void {
		switch (position) {
			case 0:
				this.y = 20;
			case 1:
				this.y = 240;
			default:
				this.y = 430;
		}
		if (showSkin) {
			bg.visible = true;
		}else {
			bg.visible = false;
		}
	}
	
	public function showSelect(strArr:Array<String>):Void {
		mainText.htmlText = "";
		var str:String = strArr[0];
		for (i in 1...strArr.length) {
			var index:String = String.fromCharCode(i + 64);
			str += "<br><a href=\'event:" + i + "\'>    " + index + ". " +strArr[i] + "</a>";
		}
		mainText.htmlText = str;
		mainText.addEventListener(TextEvent.LINK, linkHandler);
	}
	
	private function linkHandler(e:TextEvent):Void {
		mainText.removeEventListener(TextEvent.LINK, linkHandler);
		var index:Int = Std.parseInt(e.text);
		dispatchEvent(new SelectEvent(index));
	}
	
	public function showText(str:String):Void {
		downArrow.visible = false;
		isComplete = false;
		tmpText = text = str;
		blockText = "";
		mainText.text = "";
		timer.addEventListener(TimerEvent.TIMER, onTimer);
		timer.start();
	}
	
	private function showComplete():Void {
		downArrow.visible = bg.visible;
		isComplete = true;
	}
	
	public function onClick():Void {
		timer.removeEventListener(TimerEvent.TIMER, onTimer);
		timer.stop();
		colorReg = ~/<#(0[xX][a-fA-F0-9]{6})>(.+?)<#>/g;
		mainText.text = colorReg.replace(text, "$2");
		colorReg = ~/<#(0[xX][a-fA-F0-9]{6})>(.+?)<#>/;
		while (colorReg.match(text)) {
			var start:Int = colorReg.matchedPos().pos;
			var end:Int = start + colorReg.matchedPos().len -14;
			text = colorReg.replace(text, "$2");
			tf.color = Std.parseInt(colorReg.matched(1));
			mainText.setTextFormat(tf, start, end);
		}
		showComplete();
	}
	
	private function onTimer(e:TimerEvent):Void {
		if (blockText.length > 0) {
			mainText.appendText(blockText.charAt(0));
			mainText.setTextFormat(tf, mainText.text.length - 1);
			blockText = blockText.substr(1);
		}else if(tmpText.length > 0){
			colorReg = ~/<#(0[xX][a-fA-F0-9]{6})>(.+?)<#>/;
			tf.color = defaultColor;
			if (colorReg.match(tmpText)) {
				var pos:Int = colorReg.matchedPos().pos;
				if ( pos > 0) {
					blockText = tmpText.substring(0, pos);
					tmpText = tmpText.substr(pos);
				}else {
					tf.color = Std.parseInt(colorReg.matched(1));
					blockText = colorReg.matched(2);
					tmpText = colorReg.replace(tmpText, "");
				}
			}else {
				blockText = tmpText;
				tmpText = "";
			}
		}else {
			timer.removeEventListener(TimerEvent.TIMER, onTimer);
			timer.stop();
			showComplete();
		}
	}
}